
extern "C" {
#include <project.h>

    /** /#define DEBUG_SPCOMMMSH*/
#ifdef DEBUG_SPCOMMMSH
#define DEBUG_95
#endif
#include <define.h>
#include <transact.h>
#include <tct.h>
#include <trt.h>
#include <pct.h>
#include <pform.h>
#include <msg.h>
#include <print.h>
#include <fields.h>
#include <comm.h>
#include <ui.h>
#include <stdio.h>
#include <modem.h>
#include <XModem.h>
    //#include <APACSmdm.h>
#include <vidnld.h>

    extern TRANSACTION_OBJECT* pobTranSERF;
}
// Predator extern int   load_keypad (INT which_keypad, INT restore, char *scf_name);
#include "CommsHandlerAll.h"
#include "serf.h"
#include "txoserf.h"

#include "UCLActivator.h"

SoftPayBaseCommsHandler::SoftPayBaseCommsHandler()
    : CommsHandler()
{
    clearSeen = 0;
}

int SoftPayBaseCommsHandler::attemptOutwardCommunication() {
    return CommsHandler::attemptOutwardCommunication();
}

void SoftPayBaseCommsHandler::supplyPobTran(TRANSACTION_OBJECT* pobTran) {
    this->pobTran = pobTran; 
    pobTranSERF = pobTran;
}

int SoftPayBaseCommsHandler::checkStatusPreComms() {
    int ret;

    if (fGetOffline(pobTran)) {
        // do offline here
        ret = TRUE;
    } else
        ret = CONTINUE_PROCESS;
    return ret;
}

int SoftPayBaseCommsHandler::checkStatusPostComms(int retVal) {
    if (retVal == ERROR_OCCURRED) {
        /** /pdebug (("--inStatus is ERR--"));*/
        if (fGetPrintReceipt(pobTran)) {
            if (fGetPrePrint() && fGetPrintReceipt(pobTran) && (inGetPrinterType() != NOPRINTER && fGetEnablePrinter()))
                inPrintError(pobTran, TRANS_FAIL_ERR_MSG21, VS_TRUE, &obPrinter);
        }
        return (ERROR_OCCURRED);
    } else
        inSavDupCheck(pobTran);

#ifdef  COUNTER_DEFINED

    lnTimer = inSetTimerOnOff(STOP_TIMER_2);

    if (lnTimer < 8L)
        obCounter.inType = RESP_TIME_LI_1;
    else if (lnTimer < 11L)
        obCounter.inType = RESP_TIME_LI_2;
    else if (lnTimer < 21L)
        obCounter.inType = RESP_TIME_LI_3;
    else
        obCounter.inType = RESP_TIME_LI_4;

    inIncCounter(&obCounter, 1);

#endif  /* COUNTER_DEFINED */
    return retVal;
}

int SoftPayBaseCommsHandler::isRetryConnect() {
    //pdebug (("--isRetryConnect--"));
    return FALSE;
}

int SoftPayBaseCommsHandler::checkIfDemoMode() {
    int ret;

    if (fGetDemoMode())
        ret = TRUE;
    else
        ret = FALSE;
    return ret;
}

void SoftPayBaseCommsHandler::initConnectSequence() {
    clearSeen = 0;
    ucl->transactSelectUCLObject();
}

int SoftPayBaseCommsHandler::performDemoMode() {
    int ret;

    if (inDemoFunction(pobTran) == VS_SUCCESS)
        ret = TRUE;
    else
        ret = FALSE;
    return ret;
}

int SoftPayBaseCommsHandler::disConnect() {
    int ret = TRUE;

    if (obCommunication.inEnd)
        if (obCommunication.inEnd(&obCommunication) != VS_SUCCESS)
            ret = ERROR_OCCURRED;
    return ret;
}

int SoftPayBaseCommsHandler::attemptConnect() {
    int retVal = TRUE;

    if (obCommunication.inBegin && !pobTran->fPredialed) {
        if ((connectStatus = obCommunication.inBegin(&obCommunication, inGetHostHandle(), VS_TRUE)) < VS_SUCCESS)
            retVal = ERROR_OCCURRED;
    }
    pobTran->fPredialed = VS_FALSE;
    return retVal;
}



int SoftPayBaseCommsHandler::checkConnectionStatus() {
    int retVal = TRUE;

    if (obCommunication.inCheck) {
        if (obCommunication.inCheck(&obCommunication) != VS_SUCCESS) {
            if (lnGGErrorMessage() == VS_FALSE) {
                vdSetTranError(CONNECT_ERR_MSG);
                //FOR Predator   load_keypad(2,0,0);    // Enabling the keypad on connect error message
            }
            retVal = ERROR_OCCURRED;
        }
    }

    //pdebug (("checkConnectionStatus() = %d",retVal));
    return retVal;
}


int SoftPayBaseCommsHandler::checkInwardConnectionStatus() {
    MODEM_OBJECT* obModem;
    char hungUp = 0;
    int retVal = VS_FALSE;
    //TXOTimer timer();


    connectStatus = retVal;
    //SVC_WAIT(5000);
    obModem = (MODEM_OBJECT *) obCommunication.Data;
    if (obModem->hModem == 0) {
        obModem->hModem = open(MDM_PORT, 0);                                 
        mdmOpened = 1;
    }

    retVal = TRUE;
    connectStatus = retVal;
    return retVal;
}

int SoftPayBaseCommsHandler::checkStatusPreCommsInward() {
    mdmOpened = 0;
    return CONTINUE_PROCESS;
}

void SoftPayBaseCommsHandler::disConnectInwards() {
    MODEM_OBJECT* obModem;

    if (connectStatus == TRUE) {
        obModem = (MODEM_OBJECT *) obCommunication.Data;
        //inFlushModemCommunication();
        if (inForceHangup(&obCommunication) == VS_SUCCESS) {
            //inOpenMDMCommun (&obCommunication);
            inAutoAnswerInit(pobTran);

            obCommunication.fLinkUp = VS_FALSE;
            /*SVC_WAIT(100);
            inFlushModemCommunication();*/
        } else {
            //Applog ("Error closing comms, fcn BeginComms");
            //retcode  = -1;
        }
        /*if(mdmOpened)
          {
            close(obModem->hModem);
            obModem->hModem = 0;
            mdmOpened = 0;
          }*/
    }
}

int SoftPayBaseCommsHandler::checkinwardsStatus() {
    unsigned char stat_map;
    MODEM_OBJECT* obModem;


    //vdInitializeModemComm (&obCommunication);
    //obCommunication.inInitialize (&obCommunication);
    obModem = (MODEM_OBJECT *) obCommunication.Data;
    obCommunication.fLinkUp = VS_FALSE;

    if (obModem->hModem == 0) {
        obModem->hModem = open(MDM_PORT, 0);                                 
        mdmOpened = 1;
    }

    stat_map = MDM_CTS;
    if (xmdm_check_status(obModem->hModem, stat_map) != 1)
        return ERROR_OCCURRED;
    stat_map = MDM_DCD;
    if (xmdm_check_status(obModem->hModem, stat_map) != 1)
        return ERROR_OCCURRED;
    obCommunication.fLinkUp = VS_TRUE;
    inFlushModemCommunication();
    return TRUE;
}
